﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Copad.BusinessCard.DAL;
using Copad.BusinessCard.Model;
using System.Net;
using System.Linq.Expressions;

namespace Copad.BusinessCard.BLL
{
    public class CategoryBLL : BLL<Category>
    {
        UserGroupBLL userGroupBLL = new UserGroupBLL();
        CategoryGroupPermissionsBLL categoryGroupPermissionsBLL = new CategoryGroupPermissionsBLL();
        CategoryUserPermissionsBLL categoryUserPermissionsBLL = new CategoryUserPermissionsBLL();

        public List<CategoryStatisticks> GetStatisticks(UserInfo userInfo)
        {
            var categories = dataBase.Select<Category>();
            var groupeds = dataBase.Select<CardCategory>();
            var cardids = (from x in groupeds select x.CardID).Distinct();

            List<CategoryStatisticks> list = new List<CategoryStatisticks>();
            var gid = (from x in userInfo.Groups select x.ID).ToList();
            var deniedArray = categoryGroupPermissionsBLL.Get(w => gid.Contains(w.GroupID));
            var deniedUserArray = categoryUserPermissionsBLL.Get(w => w.UserID == userInfo.User.ID);

            if (deniedArray.Where(x => x.CategoryID == 0).FirstOrDefault() == null && deniedUserArray.Where(x => x.CategoryID == 0).FirstOrDefault() == null)
            {
                CategoryStatisticks ungrouped = new CategoryStatisticks { ID = 0, CategoryID = "" };
                ungrouped.Name = "未分组";
                ungrouped.Count = dataBase.Count<CardInfo>(w => !cardids.Contains(w.CardID));
                list.Add(ungrouped);
            }

            categories.ToList().ForEach(x =>
            {
                if (deniedArray.Count(w => w.CategoryID == x.ID) == 0 && deniedUserArray.Where(w => w.CategoryID == x.ID).FirstOrDefault() == null)
                {
                    CategoryStatisticks cs = new CategoryStatisticks { ID = x.ID, CategoryID = x.CategoryID, Name = x.Name };
                    cs.Count = dataBase.Count<CardCategory>(w => w.CategoryID == x.CategoryID);
                    list.Add(cs);
                }
            });
            return list;
        }

        public List<CategoryPermissions> GetCategoryPermissions()
        {
            List<CategoryPermissions> list = new List<CategoryPermissions>();

            var categories = dataBase.Select<Category>();
            var groupeds = dataBase.Select<CardCategory>();
            var cardids = (from x in groupeds select x.CardID).Distinct();
            var categoryGroupPermissions = dataBase.Select<CategoryGroupPermissions>().ToList();
            var categoryUserPermissions = dataBase.Select<CategoryUserPermissions>().ToList();

            CategoryPermissions ungrouped = new CategoryPermissions { ID = 0, CategoryID = "" };
            ungrouped.Name = "未分组";
            ungrouped.Count = dataBase.Count<CardInfo>(w => !cardids.Contains(w.CardID));
            ungrouped.GroupsID = string.Join(",", from x in categoryGroupPermissions.Where(w => w.CategoryID == 0) select x.GroupID);
            ungrouped.UsersID = string.Join(",", from x in categoryUserPermissions.Where(w => w.CategoryID == 0) select x.UserID);
            list.Add(ungrouped);

            categories.ToList().ForEach(x =>
            {
                CategoryPermissions cs = new CategoryPermissions { ID = x.ID, CategoryID = x.CategoryID, Name = x.Name };
                cs.Count = dataBase.Count<CardCategory>(w => w.CategoryID == x.CategoryID);
                cs.GroupsID = string.Join(",", from xx in categoryGroupPermissions.Where(w => w.CategoryID == x.ID) select xx.GroupID);
                cs.UsersID = string.Join(",", from xx in categoryUserPermissions.Where(w => w.CategoryID == x.ID) select xx.UserID);
                list.Add(cs);
            });
            return list;
        }

        public void UpdatePermissions(long categoryID, List<long> userID, List<long> groupID)
        {
            dataBase.BeginTransaction();
            dataBase.Delete<CategoryGroupPermissions>(x => x.CategoryID == categoryID);
            groupID.ForEach(x =>
            {
                CategoryGroupPermissions permissions = new CategoryGroupPermissions { CategoryID = categoryID, GroupID = x };
                dataBase.Insert(permissions);
            });
            dataBase.Delete<CategoryUserPermissions>(x => x.CategoryID == categoryID);
            userID.ForEach(x =>
            {
                CategoryUserPermissions permissions = new CategoryUserPermissions { CategoryID = categoryID, UserID = x };
                dataBase.Insert(permissions);
            });
            dataBase.EndTransaction();
        }

        /// <summary>
        /// 同步分组数据
        /// </summary>
        public void Sync(IList<Category> categories)
        {
            dataBase.BeginTransaction();
            foreach (Category x in categories)
            {
                string syncID = new string(x.CategoryID.ToCharArray());
                var category = dataBase.Select<Category>(y => y.CategoryID == syncID).FirstOrDefault();
                if (category == null)
                {
                    dataBase.Insert(x);
                }
                else
                {
                    category.Name = x.Name;
                    dataBase.Update(category);
                }
            }

            var existsCategories = dataBase.Select<Category>();
            var existsSyncID = from xx in categories select xx.CategoryID;
            var deleted = existsCategories.Where(x => !existsSyncID.Contains(x.CategoryID));
            deleted.ToList().ForEach(x =>
            {
                dataBase.Delete(x);
            });
            dataBase.EndTransaction();
        }
    }
}
